package cn.edu.swu.book;

import cn.edu.swu.common.HTMLset;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;

@WebServlet("/Update.html")
public class UpdateBookHtml extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("bookId");
        try{
            BOOK book=bookApplication.getInstance().getBookById(id);
            String template= """
                    <br>
                        <form action="./AddBook" method="post" enctype="multipart/form-data">
                        <input type="hidden" name="id" value="%s">
                            书 名：<input type="text" name="name" value="%s"><br><br>
                            作 者：<input type="text" name="author" value="%s"><br><br>
                            内 容：<textarea cols="30" rows="10" name="content">%s</textarea><br><br>
                            价 格：<input type="text" name="price" value="%s"><br><br>
                            图 片：<input type="file" name="picture"><br><br>
                            <input type="submit" value="提 交">
                        </form>
                        <br>
                    """;
            String form=String.format(template,book.getId(),book.getName(),book.getAuthor(),book.getContent(),book.getPrice());
            String html= HTMLset.wrap(form);
            resp.setContentType("text/html");
            try(Writer writer=resp.getWriter()){
                writer.write(html);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
